package com.widget.wallpaper;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;


public class WNWConfig extends Activity{
	private static final String PREFS_NAME = "com.widget.wallpaper.WNWProvider";
	private int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
	private String mCharacter = "penguin";

	// Preferences
	private static final String PREF_CHAR_KEY = "character";
	public static final String PREF_CHARACTER = "penguin";
	private String isInputBoxDisplay = "FALSE";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.configure);

		
		
		// Find the widget id from the intent.
		Intent intent = getIntent();
		Bundle extras = intent.getExtras();
		if (extras != null) {
			mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
					AppWidgetManager.INVALID_APPWIDGET_ID);
			if(extras.getString("INTENT_SHOW_INPUT")!=null)
					isInputBoxDisplay =extras.getString("INTENT_SHOW_INPUT");
			Log.d("character",isInputBoxDisplay);
			
		}

		// Bind the action for the save button.
		findViewById(R.id.save_button).setOnClickListener(mOnClickListener);
		
		final RadioButton radioPenguin = (RadioButton) findViewById(R.id.character1);
		final RadioButton radioWhite = (RadioButton) findViewById(R.id.character0);
		final RadioButton radioBlack = (RadioButton) findViewById(R.id.character2);
		
		final Context context = WNWConfig.this;
		String currentCharacter = WNWConfig.getCharacterPref(context);
		if(currentCharacter.equalsIgnoreCase("penguin"))
		{
			radioPenguin.setChecked(true);
			radioWhite.setChecked(false);
			radioBlack.setChecked(false);
			mCharacter = "penguin";
		}
		if(currentCharacter.equalsIgnoreCase("white"))
		{
			radioPenguin.setChecked(false);
			radioWhite.setChecked(true);
			radioBlack.setChecked(false);
			mCharacter = "white";
		}
		if(currentCharacter.equalsIgnoreCase("black"))
		{
			radioPenguin.setChecked(false);
			radioWhite.setChecked(false);
			radioBlack.setChecked(true);
			mCharacter = "black";
		}
		
		//blinding radio buttons
		radioPenguin.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if(radioPenguin.isChecked())
				{
					radioWhite.setChecked(false);
					radioBlack.setChecked(false);
					mCharacter = "penguin";
				}
			}
		});
		radioWhite.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if(radioWhite.isChecked())
				{
					radioPenguin.setChecked(false);
					radioBlack.setChecked(false);
					mCharacter = "white";
				}
			}
		});
		radioBlack.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if(radioBlack.isChecked())
				{
					radioPenguin.setChecked(false);
					radioWhite.setChecked(false);
					mCharacter = "black";
				}
			}
		});

		



	}
	private OnClickListener mOnClickListener = new OnClickListener() {
		public void onClick(View v) {
			final Context context = WNWConfig.this;

			// When the button is clicked, save our prefs and return that they
			// clicked OK.

			saveCharacterPref(context, mAppWidgetId, mCharacter);

			// Push widget update to surface with newly set prefs
			Intent iStartService =new Intent(context, WNWService.class);
			iStartService.putExtra("INTENT_SHOW_INPUT", isInputBoxDisplay);
			context.startService(iStartService);

			// Make sure we pass back the original appWidgetId
			Intent resultValue = new Intent();
			resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
					mAppWidgetId);
			setResult(RESULT_OK, resultValue);
			finish();
			
		}
	};
	public static String getCharacterPref(Context context) {
		SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
		Log.d("character",PREF_CHARACTER);
		return prefs.getString(PREF_CHAR_KEY, PREF_CHARACTER);
	}
	private void saveCharacterPref(Context context, int appWidgetId, String character) {
		SharedPreferences.Editor prefs = context.getSharedPreferences(
				PREFS_NAME, 0).edit();
		prefs.putString(PREF_CHAR_KEY, character);
		prefs.commit();
	}
}
